<?php

class Application_Model_Mapper_AenderungUnterkunftMapper{
	protected $_dbTable;

	public function setDbTable ($dbTable){
		if (is_string($dbTable)){
			$dbTable = new $dbTable();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract){
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable (){
		if (null === $this->_dbTable){
			$this->setDbTable('Application_Model_DbTable_aenderungunterkunft');
		}
		return $this->_dbTable;
	}

	public function add (Application_Model_Unterkunft $object, $originalID){
		$now = new DateTime();
		/**
		 * Fill data array
		 */
		
		$generatedid = Helper_Idgenerator::getid();
		$generatedidcheck = $this->fetchID($generatedid);
		if(empty($generatedidcheck)){
			$data = array(
					'id' =>$generatedid,
					'originalID'=>$originalID,
					'created' => date('Y-m-d H:i:s'),
					'lastupdater'=> $object->getLastupdater(), //id von ersteller mitschicken
					'lastupdated'=> date('Y-m-d H:i:s'),
					'name' => $object->getName(),
					'revier' => $object->getRevier(),
					'land' => $object -> getLand(),
					'stadt' => $object->getStadt(),
					'adresse' => $object->getAdresse(),
					'lat' =>$object->getLat(),
					'lng' =>$object->getLng(),
					'saisonzeitbeginn' => $object->getSaisonzeitbeginn(),
					'saisonzeitende' => $object->getSaisonzeitende(),
					'hafenweg' => $object->getHafenweg(),
					'open' => $object->getOpen(),
					'close' => $object->getClose(),
					'weblink' => $object->getWeblink(),
					'premium' => $object->getPremium(),
					'status' => $object->getStatus()
// 	 				'profilbild' => $object->getProfilbild()
			);
			return $this->getDbTable()->insert($data);
		}
		else{
			$this->add($object);
		}
	}

	public function delete ($id){
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('id = ?', $id);
		$table->delete($where);
	}

	public function fetchID ($id){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('aenderungunterkunft');
		$entry->where('aenderungunterkunft.id = ?', $id);
		return $entry->query()->fetch();
	}

	public function fetchAll(){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('aenderungunterkunft');
		return $entry->query()->fetchall();
	}
}